package com.example.androidautoproject.utils

import android.content.Context
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.graphics.Rect
import android.util.Base64
import android.util.Log
import android.view.accessibility.AccessibilityNodeInfo
import java.io.ByteArrayOutputStream
import java.io.File
import java.io.FileOutputStream

object ScreenshotUtil {
    var screenshot: File? = null
    var screenshotPath: String? = ""
    var bitImage: Bitmap? = null
    var base64: String? = ""

    // 图片文件路径 转 Bitmap
    fun filePathToBitmap(path: String): Bitmap{
        return BitmapFactory.decodeFile(path)
    }

    // 图片文件路径 转 Base64
    fun filePathToBase64(path: String):String{
        val bitmap = BitmapFactory.decodeFile(path)
        return bitmapToBase64(bitmap)
    }

    // Bitmap 转 Base64
    fun bitmapToBase64(bitmap: Bitmap):String{
        val outputStream = ByteArrayOutputStream()
        bitmap.compress(Bitmap.CompressFormat.PNG, 100, outputStream)
        val byteArray = outputStream.toByteArray()
        return Base64.encodeToString(byteArray, Base64.DEFAULT)
    }

    // Base64 转 Bitmap
    fun base64ToBitmap(base64Str: String): Bitmap {
        val decodedBytes = Base64.decode(base64Str, Base64.DEFAULT)
        return BitmapFactory.decodeByteArray(decodedBytes, 0, decodedBytes.size)
    }

    // Bitmap 转 图片文件路径（保存为文件）
    fun bitmapToFile(bitmap: Bitmap, filePath: String) {
        val file = File(filePath)
        val outputStream = FileOutputStream(file)
        bitmap.compress(Bitmap.CompressFormat.PNG, 100, outputStream)
        outputStream.flush()
        outputStream.close()
    }

    // Base64 转 图片文件路径
    fun base64ToFile(base64Str: String, filePath: String) {
        val bitmap = base64ToBitmap(base64Str)
        bitmapToFile(bitmap, filePath)
    }

    // 保存 Bitmap 到内部存储
    fun saveBitmapToInternalStorage(context: Context, bitmap: Bitmap, fileName: String): String {
        val file = File(context.filesDir, fileName)
        val outputStream = FileOutputStream(file)
        bitmap.compress(Bitmap.CompressFormat.PNG, 100, outputStream)
        outputStream.flush()
        outputStream.close()
        return file.absolutePath
    }

    // 怎么从内部存储中读取图片到bitmap
    fun loadBitmapFromInternalStorage(context: Context, fileName: String): Bitmap? {
        val file = File(context.filesDir, fileName)
        return if (file.exists()) {
            BitmapFactory.decodeFile(file.absolutePath)
        } else {
            null
        }
    }

    //怎么根据无障碍服务节点来裁剪bitmap图片
    fun cropNodeBitmap(screenBitmap: Bitmap, nodeInfo: AccessibilityNodeInfo): Bitmap? {
        val rect = Rect()
        nodeInfo.getBoundsInScreen(rect)
        // 检查裁剪区域是否在 Bitmap 范围内
        if (rect.left < 0 || rect.top < 0 ||
            rect.right > screenBitmap.width || rect.bottom > screenBitmap.height) {
            return null
        }
        return Bitmap.createBitmap(
            screenBitmap,
            rect.left,
            rect.top,
            rect.width(),
            rect.height()
        )
    }

    fun cropNodeByPath(path: String, left: Int, top: Int, right: Int, bottom: Int) :String{
        Log.e("ServerProject", "cropNodeByPath  ${path} ${left} ${top} ${right} ${bottom}")
        val bitmap = BitmapFactory.decodeFile(path)
        val width = right - left
        val height = bottom - top
        val nodeBitMap = Bitmap.createBitmap(
            bitmap,
            left,
            top,
            width,
            height
        )
        Log.e("ServerProject", "cropNodeByPath nodeBitMap  ${nodeBitMap} ")
        return bitmapToBase64(nodeBitMap)
    }


    // 怎么判断screenBitmap 是否和屏幕分辨率一致
    fun isBitmapMatchScreen(context: Context, screenBitmap: Bitmap): Boolean {
        val metrics = context.resources.displayMetrics
        val screenWidth = metrics.widthPixels
        val screenHeight = metrics.heightPixels
        return screenBitmap.width == screenWidth && screenBitmap.height == screenHeight
    }

    // 获取中心点坐标
    fun getCenterPoint(nodeInfo: AccessibilityNodeInfo){
        val rect = Rect()
        nodeInfo.getBoundsInScreen(rect)
        val centerX = rect.left + rect.width() / 2
        val centerY = rect.top + rect.height() / 2
    }
}